//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;
using Polymorph.Properties;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeArgumentReference"/> class represents a reference to an
    /// argument passed into a method.
    /// </summary>
    public class CodeArgumentReference : CodeValueReference
    {
        private Type m_type;
        private int m_argIndex;
        private string m_name;

        /// <summary>
        /// Initializes a new instance of <see cref="CodeArgumentReference"/>.
        /// </summary>
        /// <param name="type">The type of the argument.</param>
        /// <param name="argIndex">The argument index.</param>
        /// <param name="name">The name of the argument.</param>
        public CodeArgumentReference(Type type, int argIndex, string name)
        {
            m_type = type;
            m_argIndex = argIndex;
            m_name = name;
        }

        /// <summary>
        /// Generates the IL for an argument reference.
        /// </summary>
        /// <param name="gen">The IL generator to use.</param>
        public override void Generate(ILGenerator gen)
        {
            switch (m_argIndex)
            {
                case 0: gen.Emit(OpCodes.Ldarg_0); break;
                case 1: gen.Emit(OpCodes.Ldarg_1); break;
                case 2: gen.Emit(OpCodes.Ldarg_2); break;
                case 3: gen.Emit(OpCodes.Ldarg_3); break;
                default:
                    if (m_argIndex <= byte.MaxValue)
                        gen.Emit(OpCodes.Ldarg_S, (byte)m_argIndex);
                    else
                        gen.Emit(OpCodes.Ldarg, m_argIndex);
                    break;
            }
        }

        public override void GenerateSet(ILGenerator gen, CodeExpression value)
        {
            value.Generate(gen);
            //TODO ndarnell check to make sure safe conversions deals with the destination 
            // being a pass by reference.
            CodeGenerationHelper.GenerateSafeConversion(gen, m_type, value.ResultType);

            if (m_type.IsByRef)
            {
                CodeGenerationHelper.StoreAtAddress(gen, m_type);
            }
            else
            {
                if (m_argIndex <= byte.MaxValue)
                    gen.Emit(OpCodes.Starg_S, (byte)m_argIndex);
                else
                    gen.Emit(OpCodes.Starg, m_argIndex);
            }
        }

        /// <summary>
        /// Prints the argument reference in a human readable format.
        /// </summary>
        /// <param name="cw">The code writer to use.</param>
        public override void PrintCode(CodeWriter cw)
        {
            cw.Write(m_name);
        }

        /// <summary>
        /// Gets the type of the argument reference.
        /// </summary>
        public override Type ResultType
        {
            get { return m_type; }
        }
    }
}